using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MSRI.SmsService;
using MSRI.HelperClasses;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.Remoting.Messaging;
using System.Threading;
namespace SMS_via_Excel_Sheet
{
    public partial class Form1 : Form
    {
        public SmsServiceClass SmsService = null;
        delegate void UpdateListBoxDelegate(Guid guid);
        private Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = null;

        public Form1()
        {
            InitializeComponent();
            try
            {
                SmsService = new SmsServiceClass("SMS_via_Excel_Sheet");
                SmsService.OnDeliveryReportReceived += new OnDeliveryReportDelegate(SMSService_OnDeliveryReportReceived);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Application.Exit();
            }
        }


        //
        // Your Code for Processing a deliveryreport goes here
        //
        void SMSService_OnDeliveryReportReceived(DeliveryReportClass DeliveryReport)
        {
            UpdateListBox(DeliveryReport.MessageID);
        }

        private void UpdateListBox(Guid guid)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateListBoxDelegate(UpdateListBox), new object[] { guid });
            }
            else
            {
                foreach (ListViewItem lvi in NameAndNumberListBox.Items)
                {
                    if (lvi.SubItems[3].Text == guid.ToString())
                        lvi.SubItems[2].Text = "Sent";
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = "*.xls;*.xlsx"; 
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // Here is the call to Open a Workbook in Excel 
                // It uses most of the default values (except for the read-only which we set to true)
                FileNameBox.Text = openFileDialog1.FileName;
                ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Excel.Workbook theWorkbook = ExcelApp.Workbooks.Open
                    (openFileDialog1.FileName, 0, true, 5,"","",
                            true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true,true,false);
                Excel.Worksheet workSheet = (Excel.Worksheet)theWorkbook.ActiveSheet;
                try
                {
                    int row = 2;
                    NameAndNumberListBox.Items.Clear();
                    while (((Excel.Range)workSheet.Cells[row, 1]).Value2 != null)
                    {
                        string Name = ((Excel.Range)workSheet.Cells[row, 1]).Value2.ToString();
                        string Mobile = ((Excel.Range)workSheet.Cells[row, 2]).Value2.ToString();
                        NameAndNumberListBox.Items.Add(Name);
                        NameAndNumberListBox.Items[row - 2].SubItems.Add(Mobile);
                        NameAndNumberListBox.Items[row - 2].SubItems.Add("Not Sent");
                        row++;
                    }
                }
                catch
                {
                    MessageBox.Show("Error reading the Excel sheet...");
                }
                ExcelApp.Quit();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in NameAndNumberListBox.Items)
            {
                SmsClass Message = new SmsClass();
                Message.Number = lvi.SubItems[1].Text;
                Message.Body = SmsBodyBox.Text;
                lvi.SubItems.Add(Message.MessageID.ToString());
                SmsService.SendSms(Message);
            }
        }
    }
}